clear
set more 1

do $do\minwave

local w=1
local maxw=18
while `w'<=`maxw' {
	
di `w'
#delimit ;
if      `w'==1 {; local j="a"; local j1="b"; };
else if `w'==2 {; local j="b"; local j1="c"; };
else if `w'==3 {; local j="c"; local j1="d"; };
else if `w'==4 {; local j="d"; local j1="e"; };
else if `w'==5 {; local j="e"; local j1="f"; };
else if `w'==6 {; local j="f"; local j1="g"; };
else if `w'==7 {; local j="g"; local j1="h"; };
else if `w'==8 {; local j="h"; local j1="i"; }; 
else if `w'==9 {; local j="i"; local j1="j"; }; 
else if `w'==10 {; local j="j"; local j1="k"; };
else if `w'==11 {; local j="k"; local j1="l"; };
else if `w'==12 {; local j="l"; local j1="m"; };
else if `w'==13 {; local j="m"; local j1="n"; };
else if `w'==14 {; local j="n"; local j1="o"; };
else if `w'==15 {; local j="o"; local j1="p"; };
else if `w'==16 {; local j="p"; local j1="q"; };
else if `w'==17 {; local j="q"; local j1="r"; };
else if `w'==18 {; local j="r"; };
#delimit cr

if `w'==1 {
use pid `j'jbtime `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'tuin2 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */ `j'f131 `j'f132 `j'f133 `j'f134 `j'f137 `j'f139 `j'f140 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat/*
   */  `j'plnowm `j'plnowy4 `j'jbpen* `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
g asampst=1
}
else if `w'<=5 {
use pid `j'jbtime `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'tuin2 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */ `j'f131 `j'f132 `j'f133 `j'f134 `j'f137 `j'f139 `j'f140 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */  `j'sampst `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'<=7 {
use pid `j'jbtime `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'tuin2 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */ `j'f125 `j'f131 `j'f132 `j'f133 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */ `j'sampst `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'<=8 {
use pid `j'jbtime `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */  `j'f125 `j'f132 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */  `j'sampst  `j'plnowm `j'plnowy4 `j'movest `j'plnew `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'==9 {
use pid `j'jbtime `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */  `j'f125 `j'f132 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */ `j'sampst  `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'<=12 {
use pid `j'jbtime `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */  `j'f125 `j'f132 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */ `j'sampst  `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'==13 {
use pid `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */  `j'f125 `j'f132 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */ `j'sampst  `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'==14 {
use pid `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */  `j'f125 `j'f132 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */ `j'sampst  `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}
else if `w'>14 {
use pid `j'jbhrlk `j'hid `j'pno `j'doi* `j'sex `j'jbstat `j'race `j'mlstat  `j'jbsoc `j'jbsic* `j'jbsize `j'jbhrs `j'jbot `j'jbotpd `j'jbbg* `j'jbsect /*
   */ `j'tujbpl `j'tuin1 `j'jupayl `j'jupayx `j'juhrsx `j'juhrsl `j'age `j'nchild `j'region* `j'scend `j'feend `j'fetype `j'dob* `j'spjb /*
   */ `j'qfedhi `j'paygu `j'paynu `j'cjsten `j'cjswk9 `j'jtyid `j'jlyid `j'njbwks `j'njuwks `j'njiwks `j'njbnew `j'njbsp `j'njusp `j'njisp  `j'jbstatl `j'jbstatt /*
   */  `j'f125 `j'f132 `j'f137 `j'f139 `j'f140 `j'f142 `j'fimnb `j'paygui `j'fiyrdi* `j'tenure `j'mastat /*
   */ `j'sampst  `j'plnowm `j'plnowy4 `j'movest `j'plnew  `j'hhtype `j'cjsbg* `j'julk1 `j'julk4 `j'julkjb `j'juspec `j'jboff `j'jbhas using "${bhps}/`j'indresp"
}

g wave=`w'
renpfix `j'
if `w'==16 {
rename id pid
}

if `w'==1 {
g doiy=91 
replace doiy=doiy+1 if doim<9&doiy==90+wave 
}
if `w'==1 {
g doiy4=doiy+1900
}

sort hid
save ${temp}\temp1, replace
u `j'hid `j'nch02 `j'nch34 `j'nch511 `j'nch1215 `j'nch1618 `j'nkids `j'agechy `j'fihhmb `j'fihhmi `j'fihhml `j'fihhmn `j'fihhmnl using "${bhps}/`j'hhresp"
renpfix `j'
sort hid
merge m:m hid using ${temp}\temp1
drop _merge
g idate=mdy(doim,doid,doiy4)/365	

recode cjsbgy -9=. -7=. -2=. -1=.
recode cjsbgy4 -9=. -7=. -2=. -1=.	
recode cjsbgm -1=7 -9=. -7=. -2=. 13=1 14=4 15=7 16=10
replace cjsbgy4=. if cjsbgy4>2010

g cjsbg=mdy(cjsbgm,cjsbgd,cjsbgy4)/365
g cjsbg_nd=mdy(cjsbgm,1,cjsbgy4)/365
sort hid pno
save ${temp}\temp1, replace

/*** this sections prepares prev int date ***/
if `w'<`maxw' {
u pid idate using ${temp}\temp1
rename idate idatel
sort pid
save ${temp}\pidtemp`j1', replace
u pid `j1'pno `j1'hid `j1'sampst using "${bhps}/`j1'indresp"
sort pid
merge 1:m pid using ${temp}\pidtemp`j1'
drop _m
sort pid
merge pid using ${temp}\minwave
replace idatel=mdy(9,1,1989+`w')/365 if `w'==minwave
drop _merge pid
rename `j1'hid hid
rename `j1'pno pno
drop if hid==.|pno==.
sort hid pno
save ${temp}\pidtemp`j1', replace
}

u `j'hid `j'pno `j'jtyid `j'jlyid `j'jbstat `j'jbstatt using "${bhps}/`j'indresp"
renpfix `j'
drop if jlyid==0
g wave=`w'
sort hid pno
save ${temp}\temp2, replace

u `j'hid `j'pno `j'jspno `j'jhend* `j'jhstat `j'jhbg* `j'jhstp `j'jblky `j'jhsic* `j'jhgpay `j'jhnpay `j'jhsize using "${bhps}/`j'jobhist",clear
renpfix `j'

sort hid pno
merge hid pno using ${temp}\temp2
drop _merge
sort hid pno
if `w'==1 {
	g idatel=mdy(9,1,1990)/365
}
else {
merge hid pno using ${temp}\pidtemp`j'
}
save ${temp}\temp2, replace
save ${temp}\ttemp`j', replace

do ${do}\zdate

/* this merges with all spells number and pay*/

if `w'==3 {
sort hid pno
save ${temp}\temp1, replace
clear				   
do ${do}\life
sort hid pno
merge hid pno using ${temp}\temp1 
drop _m
sort hid pno
save ${temp}\temp1,replace
}

if `w'>1 {
append using ${temp}\temp
}
capture drop _m
sort pid
save ${temp}\temp, replace

local w=`w'+1 
}

u cljhad cjcebg* pid using ${bhps}\cindresp,clear
sort pid
save ${temp}\tempc, replace
u ${temp}\temp
sort pid
merge pid using ${temp}\tempc
drop _m
save ${temp}\temp, replace

replace doiy=doiy-1900 if doiy>1900

do ${do}\bhpsmv   
save ${temp}\bhpsjob, replace


/* this section codes up employment status */
recode jbstat 8=-1 7=8 6=7 5=6 if wave==1
recode jbstat -1=5 if wave==1
g emp=jbstat<=2 if jbstat~=.

recode jbstatt 8=-1 7=8 6=7 5=6 if wave==1
recode jbstatt -1=5 if wave==1
g empt=jbstatt<=2 if jbstatt~=.

recode jbstatl 8=-1 7=8 6=7 5=6 if wave==1
recode jbstatl -1=5 if wave==1
g empl=jbstatl<=2 if jbstatl~=.

capture drop _m
sort pid
merge pid using ${temp}\minwave 
sort pid wave
quie by pid: g emp_1=emp[_n-1] if wave==wave[_n-1]+1
replace emp_1=jbstatl<=2 if jbstatl~=.&wave==minwave /* lf status at first wave */

/* this section works out age left full-time education and experience */
g edagez=scend
replace edagez=feend if fetype<7
egen edagezz=max(edagez), by(pid)

g fted=jbstat==7
egen ftedz=max(age) if jbstat==7, by(pid)  /* this has problem that may never leave fted */
egen maxage=max(age), by(pid)
egen ftedzz=max(ftedz), by(pid)
replace ftedzz=ftedzz+1  /* because they must leave fted in the course of the year */
g edage=max(ftedzz,edagezz)

g exp=age-edage
replace exp=. if exp<0
egen expmin=min(exp), by(pid)
gen wave0=wave if exp==expmin
egen wave0z=min(wave0), by(pid)
g expz=expmin+wave-wave0z /* ensures experience augments by one */  
replace expz=. if expz<0
drop ftedz* edagez*

/* this section codes up whether job is new or not for wave>minwave */
g new=0 if emp==1&emp_1~=.
replace new=1 if emp==1&emp_1==0 /* workers previously not in employment */
replace new=1 if emp==1&emp_1==1&jhrec==1&jlyid>0&lt==0 /* workers in employment 
    at both interviews but job history records change of employer */
replace new=. if emp==1&emp_1==1&jhrec~=1&jlyid>0 /* workers in employment 
    at both interviews but apparent job change but no job history file */
replace new=1 if emp==1&exp==0

/* job tenure for wave 1 job if new==0 */
egen begl90=max(bgl90), by(pid)
g tenz=int(idate-begl90) if emp==1&wave==minwave&new==0
replace jbbg=jbbg/365
egen cljhadz=max(cljhad), by(pid)
replace tenz=int(idate-jbbg) if emp==1&wave==minwave&new==0&cljhadz==2

/* computes job tenure for all jobs */
replace tenz=0 if emp==1&new==1
sort pid wave
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1
quie by pid: replace tenz=tenz[_n-1]+1 if emp==1&new==0&wave==wave[_n-1]+1

/* this section codes up whether job (incl change within employer) is new or not for wave>minwave */
g newj=0 if emp==1&emp_1~=.
replace newj=0 if emp==1&jlyid==0
replace newj=1 if emp==1&emp_1==0 /* workers previously not in employment */
replace newj=1 if emp==1&emp_1==1&jhrec==1&jlyid>0&jlyid<. /* workers in employment 
    at both interviews but job history records change of employer */

replace newj=1 if emp==1&exp==0


/* job tenure for wave 1 job if new==0 */
g tenj=int(cjsten/365) if emp==1&wave==minwave&newj==0

/* computes job tenure for all jobs */
replace tenj=0 if emp==1&newj==1
	sort pid wave
quie by pid: replace tenj=int(cjsten/365) if emp==1&wave~=wave[_n-1]+1&tenj==. /*warning: some internal inconsistencies from this */
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1
quie by pid: replace tenj=tenj[_n-1]+1 if emp==1&newj==0&wave==wave[_n-1]+1

g cjstenint=int(cjsten/365)

replace prevne=. if tenz>0
replace prevne=1 if exp==0

/* procedure for checking on missing values */
g ones=1
egen minnew0=min(wave) if new==0, by(pid)
egen minnew=max(minnew0), by(pid)
egen tmiss1z=count(wave) if wave>=minnew&emp==1&tenz==., by(pid)
egen tmiss1=max(tmiss1z), by(pid) /* missing for new jobs */
egen tmiss2z=count(wave) if wave<minnew&emp==1&tenz==., by(pid)
egen tmiss2=max(tmiss2z), by(pid) /* missing cos of wave 1 info */
egen nemp=sum(emp), by(pid)
egen ntenz0=count(tenz), by(pid)
egen ntenz=max(ntenz0), by(pid)
replace tmiss1=0 if tmiss1==.&nemp>0
replace tmiss2=0 if tmiss2==.&nemp>0

/* this uses wave 3 info to get alternative measure of wave 3 job tenure */
recode cjcebgd -1=1 -9/-2=.
recode cjcebgm -1=7 -9/-7=. -2=. 13=1 14=4 15=7 16=10
recode cjcebgy -9/-1=.
g cjcebg=mdy(cjcebgm,cjcebgd,cjcebgy+1900)
g tenw3=(idate-cjcebg)/365

mvdecode doby, mv(-2)
mvdecode dobm, mv(-2)
mvdecode dobm, mv(-1)
g scyr=doby+17 if dobm>=9
replace scyr=doby+16 if dobm<9

rename race race1
egen race=max(race1), by(pid)
drop   race1

/* marriage and children */
g married=mastat==1|mastat==2  if mastat~=.
g widow=mastat==3    if mastat~=.
g divorced=mastat==4|mastat==5 if mastat~=.
g nchinf=nch02+nch34
g nchsec=nch1215+nch1618
replace nch1215=. if nch1215>100
replace nch1618=. if nch1618>100

g fmarr=(sex-1)*marr
g fnkids=(sex-1)*nkids

g qual=qfedhi
 
g ddate=idate-idatel
set more 1
compress

recode qual 1/3=1 4=2 5=1 6=2 10=2 7/9=4 11=4 12=5 13=.
tab qual, ge(qqual)

/* earnings */
*DEFINITIONS IN BHPS
*PAYGU: Usual gross pay per month: current job
*PAYNU: Usual net pay per month: current job
*JBHRS: Number of hours normally worked per week
*JUPAYX: Expected weekly net pay: jobseeker
*JUHRSX: Expected work hours: jobseeker

replace paygu=. if paygui~=0  /* removes imputed and proxy responses */
replace paygu=. if jbstat~=2
g lwg=ln(paygu)-ln(jbhrs)-ln(30/7) /*gross hourly wage*/
g lwn=ln(paynu)-ln(jbhrs)-ln(30/7) /*net hourly wage*/
g lrw=ln(jupayl)-ln(juhrsl) /* hourly reservation wage */
g lew=ln(jupayx)-ln(juhrsx) /* expected hourly wage */

replace lwg=. if lwg<ln(0.5)
replace lwg=. if lwg>ln(100)

g totwks=jbweeks+juweeks+jiweeks

replace jbweeks1=0 if jhrec==.
replace juweeks1=0 if jhrec==.
replace jiweeks1=0 if jhrec==.
replace jmweeks1=0 if jhrec==.

/** this works out years in most current spell since last idate **/
/** for those with jtyid missing  **/
sort pid wave
quie by pid: replace idatel=idate[_n-1] if wave==wave[_n-1]+1
g lag=idatel~=.
replace idatel=mdy(9,1,1990+wave-1)/365 if idatel==.
g cleng=min(cjsten,idate-idatel)/365 if cjsten~=.&idate~=.&idatel~=.
g jbweeksc=cleng if jbstat<=2
g juweeksc=cleng if jbstat==3
g jiweeksc=cleng if jbstat>3&jbstat~=.&jbstat~=5
g jmweeksc=cleng if jbstat==5
replace jbweeksc=0 if jbstat>2&jbstat~=.
replace juweeksc=0 if jbstat~=3
replace jiweeksc=0 if jbstat<4
replace jmweeksc=0 if jbstat~=5

sort pid wave
/* these are the key variables for working out time */
g jweeks=jbweeks1+jbweeksc
g uweeks=juweeks1+juweeksc
g iweeks=jiweeks1+jiweeksc
g mweeks=jmweeks1+jmweeksc
g weeksc=jbweeksc+jiweeksc+juweeksc+jmweeksc
g weeks1=jbweeks1+jiweeks1+juweeks1+jmweeks1
g weeks=jweeks+iweeks+uweeks+mweeks

g unemp=jbstat==3 if jbstat~=.
g inac=jbstat>3 if jbstat~=.

replace jspells=emp if jlyid==0
replace uspells=unemp if jlyid==0
replace ispells=inac if jlyid==0
replace nspells=1 if jlyid==0
replace recspell=0 if jlyid==0

replace jspells=emp+jspells if jhrec==0
replace uspells=unemp+uspells if jhrec==0
replace ispells=inac+ispells if jhrec==0
replace nspells=1+nspells if jhrec==0
g njobs=emp
g nemps=emp
replace njobs=emp+jspells if jhrec==1
replace nemps=emp+jspells-nsame if jhrec==1

g pj=(jweeks+mweeks)/(jweeks+iweeks+uweeks+mweeks)
g prevne1=pj<0.99 if tenz==0
replace prevne1=1 if expz==0
g chjobs=njobs>1 if njobs~=.

g empnow=emp==1 if emp~=.
g empthen=emp_1==1 if emp_1~=.
replace empthen=0 if expz==0

g break=(prevne1==1&empthen==1&empnow==1)
g ctsemp=empthen==1&empnow==1&break==0

drop if wave==.
replace lwg=. if lwg<ln(0.5)|lwg>ln(100)
replace lwg=. if emp~=1

g stexp=expz-tenz
sort pid wave
quie by pid: g tenz1=tenz[_n-1] if wave==wave[_n-1]+1
sort pid wave
quie by pid: g stexp1=stexp[_n-1] if wave==wave[_n-1]+1

sort pid wave
quie by pid: g lwg1=lwg[_n-1] if wave==wave[_n-1]+1
replace lwg=. if lwg==lwg1
drop lwg1
sort pid wave
quie by pid: g lwg1=lwg[_n-1] if wave==wave[_n-1]+1


/* these are those in employment in both periods  */
g sample=lwg~=.&lwg1~=.&empnow==1&empthen==1

/* these are those who have entered employment */
replace sample=1 if lwg~=.&empthen==0&empnow==1

/* these are thhose who have exited employment */
replace sample=1 if lwg1~=.&empthen==1&empnow==0

/* these are new entrants */
replace sample=1 if expz==0&empnow==1&lwg~=.

g esample=sample==1
replace esample=0 if expz==.

g tsample=esample
replace tsample=0 if empnow==1&esample==1&tenz==.&expz~=.
replace tsample=0 if empthen==1&esample==1&tenz1==.
replace tsample=0 if expz>45

sort pid wave
quie by pid: g tza=tsa[_n+1] if wave==1
 
g lhrs=ln(jbhrs)
sort pid wave
quie by pid: g lhrs1=lhrs[_n-1] if wave==wave[_n-1]+1
quie by pid: g lhrs2=lhrs[_n-2] if wave==wave[_n-2]+2

replace doiy4=doiy+1900 if doiy4==.
save ${data}\bhpsjob, replace
